'''
Created on 2009-11-7

@author: jazy
a wrapper for pcap funcs!!
'''
import pcap_c_funcs
from ctypes import *
import pcap_c_types
import interface

def create(source):
    '''
    DESCRIPTION
       create()  is  used to create a packet capture handle to look at packets 
       on the network.  source is a string that specifies
       the network device to open; on Linux systems with 2.2 or later kernels,
        a source argument of "any" or NULL can be used to  capture packets from 
        all interfaces.
       The  returned  handle  must  be activated with activate() before packets
        can be captured with it; options for the capture,
       such as promiscuous mode, can be set on the handle before activating it.

    RETURN VALUE
       create() returns a pcap_t * on success and NULL on failure.  If NULL is 
       returned, errbuf is filled in with an  appropriate
       error message.  errbuf is assumed to be able to hold at least
       PCAP_ERRBUF_SIZE chars.

    '''
    error=c_char_p()
    handle= pcap_c_funcs.pcap_create(source,error)
    if error.value!=None:
        raise error.value
    return handle

def activate(handle):
    '''
    DESCRIPTION
       activate()  is  used to activate a packet capture handle to look at packets
       on the network, with the options that were set
       on the handle being in effect.

    RETURN VALUE
       activate() returns 0 on success without warnings, PCAP_WARNING_PROMISC_NOTSUP
       on success on a device that doesn't  support
       promiscuous mode if promiscuous mode was requested, PCAP_WARNING on success
       with any other warning, PCAP_ERROR_ACTIVATED if the
       handle has already been activated, PCAP_ERROR_NO_SUCH_DEVICE if the capture 
       source  specified  when  the  handle  was  created
       doesn't  exist, PCAP_ERROR_PERM_DENIED if the process doesn't have permission
       to open the capture source, PCAP_ERROR_RFMON_NOT_SUP if monitor 
       mode was specified but the capture source doesn't support 
       monitor mode, PCAP_ERROR_IFACE_NOT_UP if  the  capture
       source  is  not  up,  and  PCAP_ERROR  if  another error occurred.  
       If PCAP_WARNING or PCAP_ERROR is returned, geterr() or
       perror() may be called with p as an argument to fetch or display a message 
       describing the warning or error.  If PCAP_WARN_ING_PROMISC_NOTSUP, 
        PCAP_ERROR_NO_SUCH_DEVICE,  or  PCAP_ERROR_PERM_DENIED 
        is returned, geterr() or perror() may be
       called with p as an argument to fetch or display an message giving additional
       details about the problem that  might  be  useful
       for debugging the problem if it's unexpected.

    '''
    return pcap_c_funcs.pcap_activate(handle)

def set_timeout(handle,time):
    '''
    DESCRIPTION
       set_timeout()  sets the read timeout that will be used on a capture
       handle when the handle is activated to to_ms, which is
       in units of milliseconds.

    RETURN VALUE
       a capture handle that has been activated.

    '''
    return pcap_c_funcs.pcap_set_timeout(handle,time)

def set_buffer_size(handle,size):
    '''
    DESCRIPTION
       set_buffer_size()  sets the buffer size that will be used on 
       a capture handle when the handle is activated to buffer_size,
       which is in units of bytes.

    RETURN VALUE
       set_buffer_size() returns 0 on success or PCAP_ERROR_ACTIVATED 
       if called on a capture handle that has been activated.
    '''
    return pcap_c_funcs.pcap_set_buffer_size(handle,size)

def can_set_rfmon(handle):
    '''
    DESCRIPTION
       can_set_rfmon() checks whether monitor mode could be set 
       on a capture handle when the handle is activated.
    RETURN VALUE
       set_rfmon()  returns  0 if monitor mode could not be set, 
       1 if monitor mode could be set, PCAP_ERROR_NO_SUCH_DEVICE if the
       device specified when the handle was created doesn't exist,
       PCAP_ERROR_ACTIVATED if called on a capture handle  that  has  been
       activated,  or PCAP_ERROR if an error occurred.  If PCAP_ERROR is 
       returned, geterr() or perror() may be called with p
       as an argument to fetch or display the error text.

    '''
    return pcap_c_funcs.pcap_can_set_rfmon(handle)

def set_rfmon(handle,rfmon):
    '''
    DESCRIPTION
       set_rfmon()  sets  whether  monitor mode should be set on a capture handle 
       when the handle is activated.  If rfmon is non-
       zero, monitor mode will be set, otherwise it will not be set.

    RETURN VALUE
       set_rfmon() returns 0 on success or PCAP_ERROR_ACTIVATED if called on 
       a capture handle that has been activated.

    '''
    return pcap_c_funcs.pcap_set_rfmon(handle,rfmon)

def set_promisc(handle,promisc):
    '''
    DESCRIPTION
       set_promisc() sets whether promiscuous mode should be set on a capture handle
       when the handle is activated.  If promisc is
       non-zero, promiscuous mode will be set, otherwise it will not be set.

    RETURN VALUE
       set_promisc() returns 0 on success or PCAP_ERROR_ACTIVATED if called on 
       a capture handle that has been activated.

    '''
    return pcap_c_funcs.pcap_set_promisc(handle,promisc)

def lookupdev():
    '''
    description:
        lookup valid device,return the interface name,
    return:
        the interface name,eg:eth0
    '''
    error=c_char_p()
    dev=pcap_c_funcs.pcap_lookupdev(error)
    if error.value!=None:
        raise error.value
    return dev 

def lookupnet(interface):
    '''
    decription:
        look up the the net config with interface name
    para:
        interface:the interface for lookup,eg:eth0
    return:
        ip:packed ip
        mask:packed netmask
    '''
    ip=c_int()
    mask=c_int()
    error=c_char_p()
    pcap_c_funcs.pcap_lookupnet(interface,byref(ip),byref(mask),byref(error))
    if error.value!=None:
        raise error.value
    return ip.value,mask.value

def open_live(source,snaplen,promisc,to_ms):
    '''
    description:
        open a device
    para:
        source:the device
        snamlen:the maximum number of bytes to be captured by pcap
        promisc:set to true, brings the interface into promiscuous mode 
                (however, even if it is set to false, it is possible under specific cases 
                for the interface to be in promiscuous mode, anyway)
        to_ms:the read time out in milliseconds (a value of 0 means no time out; 
              on at least some platforms, this means that you may wait 
              until a sufficient number of packets arrive before seeing any packets,
              so you should use a non-zero timeout)
    return:
        the handle,pcap
    '''
    error=c_char_p()
    handle=pcap_c_funcs.pcap_open_live(source,snaplen,promisc,to_ms,error)
    if error.value!=None:
        raise error.value
    return handle

def open_dead(linktype, snaplen):
    '''
    decription:
        open_dead() is used for creating a pcap_t structure  to  use  when
        calling the other functions in libpcap.  It is typically used when just
        using libpcap for compiling BPF code.
    para:
        linktype: specifies the link-layer type .
        snaplen: specifies the snapshot length.
    return:
        the handle of pcap
    '''
    return pcap_c_funcs.pcap_open_dead(linktype,snaplen)

def open_offline(fname):
    '''
    description:
        it is called to open a ``savefile'' for reading.
    para:
        fname:specifies the name of the file to open. The  file  has  the  same
              format  as those used by tcpdump(1) and tcpslice(1).  The name "-" in a
              synonym for stdin.
    return:
        return a pcap on  success  and  None on  failure
    '''
    error=c_char_p()
    handle=pcap_c_funcs.pcap_open_offline(fname,error)
    if error.value!=None:
        raise error.value
    return handle

def fopen_offline(fp):
    '''
    description:
        fopen_offline() to  read  dumped  data
        from  an  existing  open  stream fp.  Note that on Windows, that stream
        should be opened in binary mode.
    para:
        file pointer
    return:
        pcap handle
    '''
    error=c_char_p()
    handle=pcap_c_funcs.pcap_fopen_offline(fp,error)
    if error.value!=None:
        raise error.value
    return handle

def close(handle):
    '''
    description:
        closes  the  files  associated  with  p  and  deallocates resources.
    para:
        the pcap handle
    return:
        none
    '''
    pcap_c_funcs.pcap_close(handle)
    
def loop(handle,cnt,callback,user):
    '''
    description:
        processes packets from a live capture or ``savefile'' until
        cnt  packets are processed, the end of the ``savefile'' is reached when
        reading from a ``savefile'', breakloop() is called,  or  an  error
        occurs.   
    para:
        handle:the pcap handle
        cnt:the number of packets are processed
        callback:callback function 
        user:the user
    return:
        It does not return when live read timeouts occur.  A value of
        -1 or 0 for cnt is equivalent to infinity, so  that  packets  are  pro-
        cessed until another ending condition occurs.
        returns 0 if cnt is exhausted, -1 if an error occurs, or -2
        if the loop terminated due to a call  to  breakloop()  before  any
        packets  were  processed.
    '''
    return pcap_c_funcs.pcap_loop(handle,cnt,callback,user)

def stats(handle):
    '''
    description:
         stats()  fills in the pcap_stat structure pointed to by its second
         argument.  The values represent packet statistics from the start of the
         run to the time of the call.
         stats()  is supported only on live captures, not on ``savefiles'';
         no statistics are stored in ``savefiles'', so no statistics are  avail-
         able when reading from a ``savefile''.
    para:
        handle:the pcap handle
    return:
        stat class
    '''
    stat=pcap_c_types.pcap_stat()
    pcap_c_funcs.pcap_stats(handle,pointer(stat))
    return stat

def compile(handle,str,optimize,netmask):
    '''
    description:
        is used to compile the string str into a filter program.
    para:
        handle: pcap handle
        str: format
        optimize:optimize controls whether optimization 
                 on the resulting code is performed
        netmask:netmask  specifies  the  IPv4  netmask of 
                the network on which packets are
                being captured
    return:
        the bpf_program used by setfilter()
    '''
    fp=create_string_buffer(200,'\000')
    ret=pcap_c_funcs.pcap_compile(handle,fp,str,optimize,netmask)
    if ret==-1:
        raise "compile error"
    return fp

def setfilter(handle,fp):
    '''
    description:
        setfilter()  is used to specify a filter program.  fp is a pointer
        to a bpf_program struct, usually the result  of  a  call  to  compile().
    para:
        handle:the pcap handle
        fp:bpf_program struct ,get from compile
    return:
          setfilter()  returns  0  on  success  and -1 on failure.  If -1 is
          returned, geterr() or perror() may be called  with  p  as  an
          argument to fetch or display the error text.
    '''
    return pcap_c_funcs.pcap_setfilter(handle,fp)

def next(handle):
    '''
    description:
        reads  the  next packet (by calling dispatch() with a
        cnt of 1) and returns a u_char pointer to the data in that packet.  The
        pcap_pkthdr structure pointed to by h is filled in with the appropriate
        values for the packet.
    para:
        the handle of pcap
    return:
        the packet  and packheader
    '''
    pkthdr=pcap_c_types.pcap_pkthdr()
    pcap_c_funcs.pcap_next.restype=POINTER(c_ubyte)
    packet=pcap_c_funcs.pcap_next(handle,pointer(pkthdr))
    return packet,pkthdr

def dispatch(handle,cnt,callback,user):
    '''
    description:
        processes  packets from a live capture or ``savefile''
        until cnt packets are processed, the end of the  current  bufferful  of
        packets  is  reached  when doing a live capture, the end of the ``save-
        file'' is reached when reading from a ``savefile'', breakloop() is
        called,  or  an  error occurs.  Thus, when doing a live capture, cnt is
        the maximum number of packets to process before returning, but is not a
        minimum  number;  when  reading  a  live capture, only one bufferful of
        packets is read at a time, so fewer than cnt packets may be  processed.
     para:
         handle:the pcap handle
         cnt:cnt is the maximum number of packets 
         callback:callback function
     return:
         A  value  of  -1  or  0  for cnt causes all the packets received in one
         buffer to be processed when reading a live capture, and causes all  the
         packets in the file to be processed when reading a ``savefile''.
    '''
    return pcap_c_funcs.pcap_dispatch(handle,cnt,callback,user)


def breakloop(handle):
    '''
    description:
        breakloop()  sets  a  flag  that  will  force  dispatch()  or
        loop() to return rather than looping; they will return the  number
        of  packets  that  have been processed so far, or -2 if no packets have
        been processed so far.
        This routine is safe to use inside a signal handler on UNIX or  a  con-
        sole  control  handler  on  Windows,  as  it merely sets a flag that is
        checked within the loop.
        The flag is checked in loops reading packets from the OS - a signal  by
        itself will not necessarily terminate those loops - as well as in loops
        processing a set of packets returned by the OS.  Note that if  you  are
        catching  signals  on UNIX systems that support restarting system calls
        after a signal, and calling breakloop() in the signal handler, you
        must specify, when catching those signals, that system calls should NOT
        be restarted by that signal.  Otherwise, if the  signal  interrupted  a
        call  reading  packets  in  a  live  capture,  when your signal handler
        returns after calling breakloop(), the call will be restarted, and
        the loop will not terminate until more packets arrive and the call com-
        pletes.
        Note also that, in a  multi-threaded  application,  if  one  thread  is
        blocked    in   dispatch(),   loop(),    next(),    or
        next_ex(), a call to breakloop() in a different  thread  will
        not unblock that thread; you will need to use whatever mechanism the OS
        provides for breaking a thread  out  of  blocking  calls  in  order  to
        unblock the thread, such as thread cancellation in systems that support
        POSIX threads.
        Note that next() and next_ex() will, on some platforms,  loop
        reading  packets  from the OS; that loop will not necessarily be termi-
        nated by a signal, so breakloop()  should  be  used  to  terminate
        packet  processing even if next() or next_ex() is being used.
        breakloop() does not guarantee that no  further  packets  will  be
        processed by dispatch() or loop() after it is called; at most
        one more packet might be processed.
    para:
        handle: the pcap handle
    return:
        If -2 is returned from dispatch()  or  loop(),  the  flag  is
        cleared,  so a subsequent call will resume reading packets.  If a posi-
        tive number is returned, the flag is not cleared, so a subsequent  call
        will return -2 and clear the flag.
    '''
    pcap_c_funcs.pcap_breakloop(handle)

def setdirection(handle,direction):
    '''
    description:
        setdirection() is used to specify a direction that packets will be
        captured.    d  is  one  of  the  constants  D_IN,  D_OUT  or
        D_INOUT.  D_IN will only  capture  packets  received  by  the
        device,  D_OUT  will  only  capture packets sent by the device and
        D_INOUT will capture packets received by or sent  by  the  device.
        D_INOUT is the default setting if this function is not called.
        setdirection() isn't necessarily fully supported on all platforms;
        some platforms might return an error for all  values,  and  some  other
        platforms might not support D_OUT.
        This operation is not supported if a ``savefile'' is being read.
        setdirection()  returns  0 on success and -1 on failure. 
    para:
        handle: the pcap handle
        direction:the direction,D_INOUT,D_IN,D_OUT
    return:
         If -1 is returned, geterr() or perror() may be called  with  p  
         as  an argument to fetch or display the error text.
    '''
    return pcap_c_funcs.pcap_setdirection(handle,direction)

def getnonblock(handle):
    '''
    description:
       setnonblock() puts a capture handle into ``non-blocking'' mode, or
       takes  it  out  of ``non-blocking'' mode, depending on whether the non-
       block argument is non-zero or zero.  It has no effect on ``savefiles''.
       If  there  is  an error, -1 is returned and errbuf is filled in with an
       appropriate error message; otherwise, 0 is returned.   In  ``non-block-
       ing''  mode,  an  attempt  to  read  from  the  capture descriptor with
       dispatch() will, if no packets are currently available to be read,
       return  0  immediately  rather  than  blocking  waiting  for packets to
       arrive.  loop() and next() will not work in  ``non-blocking''
       mode.
    para:
        handle: the pcap handle
    return:
       getnonblock()  returns  the  current ``non-blocking'' state of the
       capture descriptor; it always returns 0 on ``savefiles''.  If there  is
       an  error,  -1  is returned and errbuf is filled in with an appropriate
       error message.
       errbuf is assumed to be able to hold at least PCAP_ERRBUF_SIZE chars.
    '''
    error=c_char_p()
    ret=pcap_c_funcs.pcap_getnonblock(handle,error)
    if error.value!=None:
        raise error.value
    return ret

def setnonblock(handle,nonblock):
    '''
    description:
        setnonblock() puts a capture handle into ``non-blocking'' mode, or
        takes  it  out  of ``non-blocking'' mode, depending on whether the non-
        block argument is non-zero or zero.  It has no effect on ``savefiles''.
        In  ``non-block-ing''  mode,  an  attempt  to  read  from  the  capture 
        descriptor with dispatch() will, if no packets are currently available 
        to be read,return  0  immediately  rather  than  blocking  waiting  for 
        packets to arrive.  loop() and next() will not work in  ``non-blocking''
        mode.
    para:
        handle:the pcap handle
        nonblock: nonblock mode
    return:
        If  there  is  an error, -1 is returned and errbuf is filled in with an
        appropriate error message; otherwise, 0 is returned.
    '''
    error=c_char_p()
    ret=pcap_c_funcs.pcap_setnonblock(handle,nonblock,error)
    if error.value!=None:
        raise error.value
    return ret

def perror(handle,prefix):
    '''
    description:
        perror() prints the text of the last pcap library error on stderr,
        prefixed by prefix.
    para:
        handle: the pcap handle
        prefix: the prefix string for error
    return:
        none
    '''
    pcap_c_funcs.pcap_perror(handle,prefix)

def inject(handle,buffer,size):
    '''
    description:
        inject()  sends  a  raw  packet through the network interface; buf
        points to the data of the packet, including the link-layer header,  and
        size is the number of bytes in the packet.
        Note  that,  even  if  you successfully open the network interface, you
        might not have permission to send packets on it, or it might  not  sup-
        port  sending packets; as open_live() doesn't have a flag to indi-
        cate whether to open for capturing, sending, or capturing and  sending,
        you  cannot  request  an  open that supports sending and be notified at
        open time whether sending  will  be  possible.   Note  also  that  some
        devices might not support sending packets.
        Note  that,  on  some  platforms,  the  link-layer header of the packet
        that's sent might not be the same  as  the  link-layer  header  of  the
        packet  supplied to inject(), as the source link-layer address, if
        the header contains such an address, might be changed to be the address
        assigned  to the interface on which the packet it sent, if the platform
        doesn't support sending completely raw  and  unchanged  packets.   Even
        worse,  some drivers on some platforms might change the link-layer type
        field to whatever value libpcap used when attaching to the device, even
        on  platforms  that  do  nominally  support  sending completely raw and
        unchanged packets.
    para:
        handle:the pcap handle
        buffer:the buffer we send
        size:the buffer size we send
    return:
        the size we send
    '''
    return pcap_c_funcs.pcap_inject(handle,buffer,size)

def sendpacket(handle,buffer,size):
    '''
    description:
        sendpacket() is like inject(), but it returns 0  on  success,
        rather  than  returning  the  number  of bytes written.  (inject()
        comes from OpenBSD; sendpacket() comes  from  WinPcap.   Both  are
        provided for compatibility.)
    para:
        handle:the pcap handle
        buffer:the buffer to send
        size:the buffer size to send
    return:
        inject()  returns the number of bytes written on success and -1 on
        failure.sendpacket() returns 0 on success and -1 on failure.
        If -1 is returned, geterr() or perror() may be called with  p
        as an argument to fetch or display the error text.
    '''
    return pcap_c_funcs.pcap_sendpacket(handle,buffer,size)

def strerror(error):
    '''
    description:
        convert an errno value to a string
    para:
        error number
    return:
        error string
    '''
    return pcap_c_funcs.pcap_strerror(error)

def geterr(handle):
    '''
    description:
        geterr()  returns  the  error  text  pertaining  to  the last pcap
        library error.  NOTE: the pointer it returns will no longer point to  a
        valid error message string after the pcap_t passed to it is closed; you
        must use or copy the string before closing the pcap_t.
    para:
        handle:the pcap handle
    return:
        the error string
    '''
    return pcap_c_funcs.pcap_geterr(handle)

def compile_nopcap(snaplen,linktype,str,optimize,netmask):
    '''
    description:
       compile_nopcap() is similar to compile() except that  instead
       of  passing  a  pcap  structure,  one  passes  the snaplen and linktype
       explicitly.  It is intended to be used for compiling filters for direct
       BPF  usage, without necessarily having called open().  A return of
       -1 indicates an error;  the  error  text  is  unavailable.   (com-
       pile_nopcap() is a wrapper around open_dead(), compile(), and
       close(); the latter three routines can be used directly  in  order
       to get the error text for a compilation error.)
    para:
        linktype: specifies the link-layer type .
        snaplen: specifies the snapshot length.
        str: format
        optimize:optimize controls whether optimization 
                 on the resulting code is performed
        netmask:netmask  specifies  the  IPv4  netmask of 
                the network on which packets are
                being captured
        
    return:
        -1: indicate an error
    '''
    fp=create_string_buffer(200,'\000')
    ret=pcap_c_funcs.pcap_compile_nopcap(snaplen,linktype,fp,str,optimize,netmask)
    if ret==-1:
        raise "compile error"
    return fp

def freecode(fp):
    '''
    description:
        freecode()  is  used  to  free up allocated memory pointed to by a
        bpf_program struct generated by compile() when that BPF program is
        no longer needed, for example after it has been made the filter program
        for a pcap structure by a call to setfilter().
    para:
        fp:a bpf_program struct generated by compile() 
    return:
        none
    '''
    pcap_c_funcs.pcap_freecode(fp)

def datalink(handle):
    '''
    description:
        datalink()  returns  the  link  layer type for the live capture or
       ``savefile'' specified by p.
    para:
        handle:the pcap handl
    return:
        the  link  layer type
    '''
    return pcap_c_funcs.pcap_datalink(handle)

def list_datalinks(handle):
    '''
    description:
        list_datalinks() is used to get a list of the supported data  link
        types   of   the   interface   associated  with  the  pcap  descriptor.
        list_datalinks() allocates an array to  hold  the  list  and  sets
        *dlt_buf.   The  caller  is  responsible  for  freeing  the  array with
        free_datalinks
    para:
        handle: the pcap handle
    return:
        list_datalinks() returns the number of  data  link  types  in  the
        array  on  success and -1 on failure.
    '''
    dlt_buf=POINTER(c_int)()
    ret=pcap_c_funcs.pcap_list_datalinks(handle,pointer(dlt_buf))
    if ret==-1:
        raise "list_datalinks error!"
    return ret,dlt_buf
    

def set_datalink(handle,dlt):
    '''
    description:
        set_datalink() is used to set the current data link  type  of  the
        pcap descriptor to the type specified by dlt
    para:
        handle:the pcap handle
        dlt:data link type
    return:
        set_datalink()  returns  0 on success and -1 on failure.  If -1 is
        returned, geterr() or perror() may be called  with  p  as  an
        argument to fetch or display the error text.

    '''
    return pcap_c_funcs.pcap_set_datalink(handle,dlt)

def datalink_name_to_val(name):
    '''
    description:
        datalink_name_to_val() translates a data link type name, which  is
        a  DLT_ name with the DLT_ removed, to the corresponding data link type
        value.  The translation is case-insensitive.
    para:
        name:the datalink name
    return:
        returns 0 on success and -1 on failure.

    '''
    return pcap_c_funcs.pcap_datalink_name_to_val(name)

def snapshot(handle):
    '''
    description:
        snapshot()    returns   the   snapshot   length   specified   when
        set_snapshot() or open_live() was called, for a live capture,
        or the snapshot length from the capture file, for a ``savefile''.
    para:
        handle:the pcap handle
    return:
        returns   the   snapshot   length   specified   when
        set_snapshot() or open_live() was called
    '''
    return pcap_c_funcs.pcap_snapshot(handle)

def is_swapped(handle):
    '''
    description:
        is_swapped() returns true if p refers to a ``savefile'' that  uses
        a different byte order than the current system.  For a live capture, it
        always returns false.
    para:
        handle:the pcap handle
    return:
        returns true if p refers to a ``savefile'' that  uses
        a different byte order than the current system.  For a live capture, it
        always returns false.
    '''
    return pcap_c_funcs.pcap_is_swapped(handle)

def major_version(handle):
    '''
    description:    
        If handle refers to a savefile, major_version() returns the major  num-
        ber of the file format of the savefile.If  handle  refers to a live capture, 
        the values returned by major_version() is not meaningful.
    para:
        handle: the pcap handle
    return:    
        the major number
    '''
    return pcap_c_funcs.pcap_major_version(handle)

def minor_version(handle):
    '''
    description:
       minor_version() returns
       the minor number of the file format of the savefile.  The version  num-
       ber is stored in the header of the savefile.
    para:
        handle:the pcap handle
    return:
        the minor number
    '''
    return pcap_c_funcs.pcap_minor_version(handle)

def file(handle):
    '''
    description:
        returns  the standard I/O stream of the ``savefile,'' if a
        ``savefile'' was opened with open_offline(), or NULL, if a network
        device  was  opened  with  create()  and  activate(), or with
        open_live().
        Note that the Packet Capture library is usually built with  large  file
        support,  so the standard I/O stream of the ``savefile'' might refer to
        a file larger than  2  gigabytes;  applications  that  use  file()
        should,  if  possible, use calls that support large files on the return
        value of file() or the value returned by fileno() when passed  the
        return value of file()
    para:
        handle:the pcap handle
    return:
        the file pointer
    '''
    return pcap_c_funcs.pcap_file(handle)

def fileno(handle):
    '''
    description:
        fileno()  returns  the  file descriptor number from which captured
        packets are read, if a network device was opened with create() and
        activate()  or with open_live(), or -1, if a ``savefile'' was
        opened with open_offline().
    para:
        handle: the pcap handle
    return:
        the file descriptor
    '''
    return pcap_c_funcs.pcap_fileno(handle)

def dump_open(handle,fname):
    '''
    description:
        dump_open() is called to open a ``savefile'' for  writing.   fname
        specifies  the  name  of  the file to open. The file will have the same
        format as those used by tcpdump(1) and tcpslice(1).  The name "-" in  a
        synonym for stdout.
    para:
        handle:the pcap handle
        fname: the file name
    return:
        A pointer to a pcap_dumper_t structure to use in subsequent dump()
        and  dump_close()  calls is returned on success.  NULL is returned
        on failure.  If NULL is returned, geterr(p) can be used to get the
        error text.
    '''
    return pcap_c_funcs.pcap_dump_open(fname)

def dump_fopen(handle,fp):
    '''
    description:
        dump_fopen()  is  called  to write data to an existing open stream
        fp.  Note that on Windows, that stream should be opened in binary mode.
    para:
        handle:the pcap handle
        fp: the file pointer
    return: 
        A pointer to a pcap_dumper_t structure to use in subsequent dump()
        and  dump_close()  calls is returned on success.  NULL is returned
        on failure.  If NULL is returned, geterr(p) can be used to get the
        error text.
        
    '''
    return pcap_c_funcs.pcap_dump_fopen(handle,fp)

def dump_file(dumper):
    '''
    description:
        dump_file() returns the standard I/O stream  of  the  ``savefile''
        opened by dump_open()
    para:
        dumber:the return value from the dump_opnen() or dump_fopen()
    return:
        the file pointer
    '''
    return pcap_c_funcs.pcap_dump_file(dumper)

def dump_ftell(dumper):
    '''
    description:    
        dump_ftell() returns the current file  position  for  the  ``save-
        file'',  representing  the  number of bytes written by dump_open()
        and dump().  -1 is returned on error.
    para:
        dumber:the return value from the dump_opnen() or dump_fopen()
    return:
        returns the current file  position  for  the  ``save-
        file''
    '''
    return pcap_c_funcs.pcap_dump_ftell(dumper)

def dump_flush(dumper):
    '''
    description:    
        dump_flush()  flushes  the  output  buffer to the ``savefile,'' so
        that any packets written with dump() but not yet  written  to  the
        ``savefile'' will be written.
    para:
        dumber:the return value from the dump_opnen() or dump_fopen()
    return:
        dump_flush() returns 0 on success and -1 on failure.
    '''
    return pcap_c_funcs.pcap_dump_flush(dumper)

def dump_close(dumper):
    '''
    description:
        close a savefile being written to
    para:
        dumber:the return value from the dump_opnen() or dump_fopen()
    return:
        none
    '''
    return pcap_c_funcs.pcap_dump_close(dumper)

def dump(user,pkthdr,sp):
    '''
    description:
        dump()   outputs   a   packet  to  the  ``savefile''  opened  with
        dump_open().  Note that its calling arguments are suitable for use
        with  dispatch()  or  loop().   If  called directly, the user
        parameter is of type pcap_dumper_t as returned by dump_open().
    para:
        user:the user
        pkthdr: the packet header
        sp:
    return:
        
    '''
    return pcap_c_funcs.pcap_dump(user,pkthdr,sp)

def findalldevs():
    '''
    description:
       findalldevs()  constructs  a  list  of network devices that can be
       opened with create() and activate() or with open_live().
       (Note  that  there  may be network devices that cannot be opened by the
       process calling findalldevs(), because, for example, that  process
       might not have sufficient privileges to open them for capturing; if so,
       those devices will not appear on the list.)
    para:
       none
    return:
        dev list
    '''
    alldevsp=POINTER(pcap_c_types.pcap_if)()
    p2p=pointer(alldevsp)
    error=c_char_p()
    pcap_c_funcs.pcap_findalldevs(p2p,error)
    if error.value!=None:
        raise error.value
    return interface.If(alldevsp)

def freealldevs(alldevs):
    '''
    description:
        free all devs allocated by findalldevs
    para:
        the devs allocated by findalldevs()
    return:
        none
    '''
    pcap_c_funcs.pcap_freealldevs(alldevs.pif)

def lib_version():
    '''
    description:
        get the lib version
    para:
        none
    return:
        none
    '''
    return pcap_c_funcs.pcap_lib_version()


def get_selectable_fd(handle):
    '''
    description:
            get the fd from pcap handle
    para:
        handle:the pcap handle
    return:
        the lib version string
    '''
    return pcap_c_funcs.pcap_get_selectable_fd(handle)

def statustostr(error):
    '''
    description:
         convert a PCAP_ERROR_ or PCAP_WARNING_ value to a string
    para:
        error: error number
    return:
        error string
    '''
    return pcap_c_funcs.pcap_statustostr(error)